<?php
/**
 * Created by PhpStorm.
 * User: yaobiao
 * Date: 2018/7/16
 * Time: 22:41
 */

namespace app\api\controller\v1;


use app\api\model\Jindu;
use app\api\validate\Luqu;
use app\api\model\Luqu as LuquModel;
use app\api\service\Token;

class Admission
{
    public function query()
    {
        return json(Jindu::all());
    }

    private function luquFromSuse($ksh)
    {
        $url = 'http://lqcx.suse.edu.cn/showinfo.asp';
        $data = array('idnum' => $ksh, 'bt1' => '%CC%E1++%BD%BB');
        $result = curl_request($url, '', $data);
        $result = $this->safeEncoding($result);
        if (!$result) return [
            'msg' => false, 'data' => '网络错误，请稍后再试','code' => 500
        ];
        if (strpos($result, '出错信息')) return [
            'msg' => false, 'data' => '未检索到你的信息'
        ];

        preg_match_all("/<font color=\"#FFFFFF\" face=\"黑体\" style=\"font-size: 11pt\"><b>(.*?)<\/b><\/font>/", $result, $rs);

        if (!$rs[1]) return [
            'msg' => false, 'data' => '网络错误，请稍后再试','code' => 500
        ];

        preg_match_all("/<b><font color=\"#FFFFFF\" face=\"黑体\" style=\"font-size: 11pt\">&nbsp;(.*?)<\/font><\/b>/", $result, $rs1);

        $rs1[1][0] = str_replace(' ', '', $rs1[1][0]);
        $dz = $rs1[1][0];
        $kd = '';
        if (preg_match("/EMS快递单号：(([\d]*).*)/", $rs1[1][0], $rs2)) {
            preg_match("/[\d]*(.*)/", $rs2[1], $rs3);
            $dz = $rs3[1];
            $kd = $rs2[2];

        };

        $luquData = [
            'ksh' => $rs[1][0],
            'name' => $rs[1][1],
            'zy' => $rs[1][2],
            'cc' => $rs[1][3],
            'dz' => $dz,
            'kd' => $kd,
            'sum' => 1
        ];
        //        $luquData['name'] = iconv('gb2312','utf8',$luquData['name']);

        return $luquData;
    }

    public function luqu($ksh)
    {

        (new Luqu())->goCheck();
        Token::needExclusiveScope();


        $ks = LuquModel::get($ksh);

        $new = true;
        if ($ks) {
            if ($ks->kd) {
                $ks->sum = $ks->sum + 1;
                $ks->save();
                return json([
                    'msg' => true, 'data' => $ks
                ]);
            } else {
                $new = false;
            }
        }

        $luquData = $this->luquFromSuse($ksh);

        if (array_key_exists('msg', $luquData)) {
            if (array_key_exists('code',$luquData)){
                return json($luquData,500);
            }
            return json($luquData);
        }

        if (!$new) {
            $ks->sum = $ks->sum + 1;
            $ks->dz = $luquData['dz'];
            $ks->kd = $luquData['kd'];
            $ks->save();
            return json([
                'msg' => true, 'data' => $ks
            ]);
        }

        $ksData = new LuquModel();
        $luqu = $ksData->save($luquData);
        if ($luqu) {
            return json([
                'msg' => true, 'data' => $luquData
            ]);
        }

    }

    function safeEncoding($str)
    {
        $code = mb_detect_encoding($str, array('ASCII', 'GB2312', 'GBK', 'UTF-8'));//检测字符串编码
        if ($code == "CP936") {
            $result = $str;
        } else {
            //$result=mb_convert_encoding($str,'UTF-8',$code);//将编码$code转换为utf-8编码
            $result = iconv($code, "UTF-8", $str);
        }
        return $result;
    }

}